/*Set-Up */

cap cd "C:\Users\ejm5\Dropbox\PartipationCompliance\AMJ"
cap cd "/Users/markustaussig/Dropbox/PartipationCompliance/AMJ"
set more off
pause on

use PCI2013_Domestic_Anon.dta, clear


merge 1:1 id using isic_2013_coded.dta
encode sector, gen(sector_id2)

drop _merge
sort pci_id
merge m:1 pci_id using web_2013.dta

drop _merge
sort pci_id
merge m:1 pci_id using provincial_controls.dta




generate comment_scale=0
replace comment_scale=1 if f4==1
replace comment_scale=2 if f4_2==1 & f4==1
replace comment_scale=3 if f4==1 & f4_2==1 & f4_2_2==1
tab comment_scale, gen(comment_dummy)

replace comment_dummy1=comment_dummy1*100
replace comment_dummy2=comment_dummy2*100
replace comment_dummy3=comment_dummy3*100
replace comment_dummy4=comment_dummy4*100

#delimit;
lab var comment_scale "Participation Level";
label values comment_scale comment_scale;
label define comment_scale 0 "No Participation" 1 "Provided Comment" 2 "Received Response" 3 "Comment Used";

generate collective_bargain= e17_2*100;
generate attitude_contribution=5-d14_1;
generate official_effective=5-d9_1_2;
generate attitude=5-d9_1_1;

replace e16=0 if e16==.b;

#delimit;
split answerdate, generate(date) parse(/) limit(4);
destring date1, replace;
destring date2, replace;
generate days_after=30+((date1-6)*30)+date2 if date1>=6;
replace days_after=240+date2 if date1==1;
generate ln_days=ln(days_after);
generate after=1 if days_after>=180;
replace after=0 if days_after<180;
 

/*Reviewer Suggestions*/

#delimit;
/*A. Transform DV*/
gen ln_labor=ln(formal_labor+1);

#delimit;
generate compliance=2 if formal==100;
replace compliance=1 if formal<100 & formal>=50;
replace compliance=0 if formal<50;

#delimit;
lab var compliance "Compliance Level";
label values compliance compliance ;
label define compliance 0 "Non-Compliant" 1 "Partially Compliant" 2 "Fully Compliant";

#delimit;
generate ln_recruitment=ln(e8+1);
 
saveold 20161201_AMJ.dta, replace;







/***************Figure 1 - Main AMJ Paper *********************/
#delimit;
hist  formal_labor_share if after==0, bin(10)  fcolor(gs5) lcolor(ltbluishgray) percent 
ytitle("Share of Firms (%)", size(medium) margin(medium)) xtitle("Employees with Formal Contract (%)", size(medium) margin(medium)) 
title("During Implementation Period", size(medlarge)) ylab(0(5)70,labsize(vsmall)) xlab(0(10)100,labsize(small)) scheme(s1mono);
graph save Figure3_a.gph, replace;

#delimit;
hist  formal_labor_share if after==1, bin(10)  fcolor(gs5) lcolor(ltbluishgray) percent 
ytitle("Share of Firms (%)", size(medium) margin(medium)) xtitle("Employees with Formal Contract (%)", size(medium) margin(medium)) 
title("After Implementation Period", size(medlarge)) ylab(0(5)70,labsize(vsmall)) xlab(0(10)100,labsize(small)) scheme(s1mono);
graph save Figure3_b.gph, replace;

graph combine Figure3_a.gph Figure3_b.gph,cols(2) imargin(vsmall) title("Formal Labor") scheme(s1mono);
graph save Figure3_ab.gph, replace;


#delimit;
histogram compliance if after==0, discrete percent fcolor(gs5) lcolor(ltbluishgray) barwidth(.2) xtitle("") xlabel(0(1)2, labels labsize(small) labcolor(black) angle(forty_five) valuelabel) 
ytitle("Share of Firms (%)", size(medium) margin(medium))  
title("During Implementation Period", size(medlarge)) ylab(0(5)70,labsize(vsmall)) scheme(s1mono);
graph save Figure3_c.gph, replace;

#delimit;
histogram compliance if after==1, discrete percent fcolor(gs5) lcolor(ltbluishgray) barwidth(.2) xtitle("") xlabel(0(1)2, labels labsize(small) labcolor(black) angle(forty_five) valuelabel)
ytitle("Share of Firms (%)", size(medium) margin(medium))  
title("After Implementation Period", size(medlarge)) ylab(0(5)70,labsize(vsmall)) scheme(s1mono);
graph save Figure3_d.gph, replace;

graph combine Figure3_c.gph Figure3_d.gph,cols(2) imargin(vsmall) title("Compliance Scale") scheme(s1mono);
graph save Figure3_cd.gph, replace;

graph combine Figure3_ab.gph Figure3_cd.gph, rows(2) imargin(vsmall) scheme(s1mono);
graph save Figure1.gph, replace;
graph export Figure1.pdf, as(pdf) replace;
graph export Figure1.tif, as(tif) replace;
pause;

/***************Figure 2 - Main AMJ Paper *********************/
#delimit;
graph bar (mean) formal, ytitle("Share of Workers with Contract (%)", size(medium) margin(medium)) 
over(a8_3, label(angle(forty_five) labsize(medium))) scheme(s1mono) blabel(bar, size(medsmall) color(black) position(outside) format(%4.1f)) ;
graph save Figure2.gph, replace;
graph export Figure2.pdf, as(pdf) replace;
graph export Figure2.tif, as(tif) replace;
pause;


/****************************Table 1 - Main AMJ Paper**********************************************************/

/*Factor Analysis to Create Mediation Variables*/
#delimit;
factor h1 d14_2 attitude, pcf;
rotate, varimax;
predict legitimacy ;
pause;

/****************************Table 2 - Main AMJ Paper**********************************************************/

/*Correlation and Summary Statistics*/

#delimit;
pwcorr formal compliance e16 ln_recruitment comment gov_resp effective  legitimacy  h1 d14_2 attitude  if formal_labor!=. & a5_2!=.b & a7_3 !=.b & gov_resp!=.b & emp_size!=.b, star(5);

#delimit;
sum formal compliance e16 ln_recruitment comment gov_resp effective  legitimacy  h1 d14_2 attitude  if formal_labor!=. & a5_2!=.b & a7_3 !=.b & gov_resp!=.b & emp_size!=.b;
pause;


/****************************Table 3 - Main AMJ Paper**********************************************************/

/*B. Address Selection Effects*/
#delimit;
set more off;
ebalance comment  emp_size a7_3 state_ownership equitized owner_connect former_hhe i.a4 a11_4 a11_5 a11_6;
pause;


/****************************Table 4 - Main AMJ Paper**********************************************************/

/*Just Participation Models 1 to 4*/
#delimit;
set more off;
reg formal comment after, robust ;
outreg2 using table_4, tdec(3) bdec(3) e(rmse ll) addtext(Ebalance, No, Province FE, No, Sector FE, No) replace;
reg formal comment after [aweight=_webal], robust  ;
outreg2 using table_4, tdec(3) bdec(3) e(rmse ll) addtext(Ebalance, Yes, Province FE, No, Sector FE, No) ;
areg formal comment after  [aweight=_webal], robust  absorb(pci_id) ;
outreg2 using table_4, tdec(3) bdec(3) e(rmse ll) addtext(Ebalance, Yes, Province FE, Yes, Sector FE, No) ;
areg formal  comment after  i.sector_id2 [aweight=_webal], robust  absorb(pci_id) ;
outreg2 using table_4, tdec(3) bdec(3) e(rmse ll) addtext(Ebalance, Yes, Province FE, Yes, Sector FE, Yes);
pause;

/*Government Response (Model 5)*/
#delimit;
set more off;
areg formal  gov_response  after i.sector_id2 [aweight=_webal] , robust  absorb(pci_id) ;
outreg2 using table_4, tdec(3) bdec(3) e(rmse ll) addtext(Ebalance, Yes, Province FE, Yes, Sector FE, Yes);
pause;

/*Effective (Model 6)*/
#delimit;
set more off;
areg formal effective after  i.sector_id2 [aweight=_webal] , robust  absorb(pci_id) ;
outreg2 using table_4, tdec(3) bdec(3) e(rmse ll) addtext(Ebalance, Yes, Province FE, Yes, Sector FE, Yes);
pause;

/*Just Participation Models 7 to 10*/

#delimit;
oprobit compliance comment after , ;
outreg2 using table_4, tdec(3) bdec(3) e(ll) addtext(Ebalance, No, Province FE, No, Sector FE, No);
oprobit compliance comment after [aweight=_webal], robust  ;
outreg2 using table_4, tdec(3) bdec(3) e(ll) addtext(Ebalance, Yes, Province FE, No, Sector FE, No) ;
oprobit compliance comment after i.pci_id [aweight=_webal], robust  ;
outreg2 using table_4, tdec(3) bdec(3) e(ll) addtext(Ebalance, Yes, Province FE, Yes, Sector FE, No) ;
oprobit compliance  comment after i.pci_id i.sector_id2 [aweight=_webal], robust  ;
outreg2 using table_4, tdec(3) bdec(3) e(ll)  addtext(Ebalance, Yes, Province FE, Yes, Sector FE, Yes);
pause;

/*Government Response (Model 11)*/
#delimit;
set more off;
oprobit compliance gov_response after i.pci_id i.sector_id2 [aweight=_webal], robust  ;
outreg2 using table_4, tdec(3) bdec(3) e(ll) addtext(Ebalance, Yes, Province FE, Yes, Sector FE, Yes);
pause;

/*Effective (Model 12)*/
#delimit;
oprobit compliance effective after i.pci_id i.sector_id2 [aweight=_webal]  ;
outreg2 using table_4, tdec(3) bdec(3) e(ll) addtext(Ebalance, Yes, Province FE, Yes, Sector FE, Yes) excel;
pause;

/****************************Table 5 - Main AMJ Paper**********************************************************/

/*Backlash Hypothesis (Models 1-3)*/
#delimit;
set more off;
areg formal  comment after  i.sector_id2 [aweight=_webal]  if gov_response==0, robust absorb(pci_id) ;
outreg2 using table_5, tdec(3) bdec(3) e(rmse ll) addtext(Ebalance, Yes, Province FE, Yes, Sector FE, Yes) replace;
areg formal  gov_response after  i.sector_id2 [aweight=_webal]  if comment==1 , robust absorb(pci_id) ;
outreg2 using table_5, tdec(3) bdec(3) e(rmse ll) addtext(Ebalance, Yes, Province FE, Yes, Sector FE, Yes);
areg formal comment gov_response  after  i.sector_id2 [aweight=_webal] , robust absorb(pci_id) ;
outreg2 using table_5, tdec(3) bdec(3) e(rmse ll) addtext(Ebalance, Yes, Province FE, Yes, Sector FE, Yes);
pause;

/*Backlash Hypothesis (Models 4-6)*/
#delimit;
oprobit compliance comment after i.pci_id i.sector_id2 [aweight=_webal]  if gov_response==0, robust ;
outreg2 using table_5, tdec(3) bdec(3) e(rmse ll) addtext(Ebalance, Yes, Province FE, Yes, Sector FE, Yes);
oprobit compliance gov_response after i.pci_id i.sector_id2 [aweight=_webal]  if comment==1, robust  ;
outreg2 using table_5, tdec(3) bdec(3) e(rmse ll) addtext(Ebalance, Yes, Province FE, Yes, Sector FE, Yes);
oprobit compliance comment gov_response after  i.pci_id i.sector_id2 [aweight=_webal] , robust ;
outreg2 using table_5, tdec(3) bdec(3) e(rmse ll) addtext(Ebalance, Yes, Province FE, Yes, Sector FE, Yes);
pause;

/*C. Alternative Dependent Variables*/

/*Union (Model 7*/
#delimit;
xi: areg e16 comment gov_response after i.sector_id2 [aweight=_webal] , robust absorb(pci_id);
outreg2 using table_5, tdec(3) bdec(3) e(rmse ll) addtext(Ebalance, Yes, Province FE, Yes, Sector FE, Yes);

/*Money Spent on Recruitment (Model 8)*/
#delimit;
xi: areg ln_recruitment comment gov_response after  i.sector_id2 [aweight=_webal], robust absorb(pci_id);
outreg2 using table_5, tdec(3) bdec(3) e(rmse ll) addtext(Ebalance, Yes, Province FE, Yes, Sector FE, Yes) excel;


/*Effects Graphs*/
#delimit;
set more off;
ebalance comment  emp_size equity state_ownership equitized owner_connect former_hhe i.a4 a11_4 a11_5 a11_6;
xi: areg formal comment gov_response i.sector_id2 [aweight=_webal], absorb(pci_id) robust;
lab var comment "Provided Comment=1";
lab var gov_resp "Received Response=1";
lab var after "After Implementation Per.=1";
coefplot, drop(_cons _Isector_id_*)
msize(large) mcolor(maroon) msymbol(diamond) xline(0) level(90) ciopts(lwidth(thick) lcolor(navy) lpattern(dash) title("Contracted Labor (%)"));
graph save dv_labor.gph, replace;

/********************************************************************************/


/****************************Table 6 - Main AMJ Paper**********************************************************/

/*Size*/
#delimit;
set more off;
ebalance comment   equity state_ownership equitized owner_connect former_hhe i.a4 a11_4 a11_5 a11_6 if emp_size<=4;
xi: areg formal comment gov_response after i.sector_id2 [aweight=_webal] if emp_size<=4, absorb(pci_id) robust;
outreg2 using table_6, tdec(3) bdec(3) e(rmse ll) addtext(Ebalance, Yes, Province FE, Yes, Sector FE, Yes) replace;

ebalance comment   equity state_ownership equitized owner_connect former_hhe i.a4 a11_4 a11_5 a11_6 if emp_size>4 & emp_size !=.;
xi: areg formal comment gov_response after i.sector_id2 [aweight=_webal] if emp_size>4 & emp_size !=., absorb(pci_id) robust;
outreg2 using table_6, tdec(3) bdec(3) e(rmse ll) addtext(Ebalance, Yes, Province FE, Yes, Sector FE, Yes);

ebalance comment   equity state_ownership equitized owner_connect former_hhe i.a4 a11_4 a11_5 a11_6 if emp_size<=4;
oprobit compliance comment gov_response after  i.pci_id i.sector_id2 [aweight=_webal] if emp_size <=4 , robust ;
outreg2 using table_6, tdec(3) bdec(3) e(rmse ll) addtext(Ebalance, Yes, Province FE, Yes, Sector FE, Yes);

ebalance comment   equity state_ownership equitized owner_connect former_hhe i.a4 a11_4 a11_5 a11_6 if emp_size>4 & emp_size !=.;
oprobit compliance comment gov_response after  i.pci_id i.sector_id2 [aweight=_webal] if emp_size >4 & emp_size !=. , robust ;
outreg2 using table_6, tdec(3) bdec(3) e(rmse ll) addtext(Ebalance, Yes, Province FE, Yes, Sector FE, Yes);

ebalance comment equity state_ownership equitized owner_connect former_hhe i.a4 a11_4 a11_5 a11_6 if emp_size<=4;
xi: areg e16 comment gov_response after i.sector_id2 [aweight=_webal] if emp_size<=4, absorb(pci_id) robust;
outreg2 using table_6, tdec(3) bdec(3) e(rmse ll) addtext(Ebalance, Yes, Province FE, Yes, Sector FE, Yes);

#delimit;
ebalance comment   equity state_ownership equitized owner_connect former_hhe i.a4 a11_4 a11_5 a11_6 if emp_size>4 & emp_size !=.;
xi: areg e16 comment gov_response after i.sector_id2 [aweight=_webal] if emp_size>4 & emp_size !=., absorb(pci_id) robust;
outreg2 using table_6, tdec(3) bdec(3) e(rmse ll) addtext(Ebalance, Yes, Province FE, Yes, Sector FE, Yes);

ebalance comment equity state_ownership equitized owner_connect former_hhe i.a4 a11_4 a11_5 a11_6 if emp_size<=4;
xi: areg ln_recruitment comment gov_response after i.sector_id2 [aweight=_webal] if emp_size<=4, absorb(pci_id) robust;
outreg2 using table_6, tdec(3) bdec(3) e(rmse ll) addtext(Ebalance, Yes, Province FE, Yes, Sector FE, Yes);

ebalance comment   equity state_ownership equitized owner_connect former_hhe i.a4 a11_4 a11_5 a11_6 if emp_size>4 & emp_size !=.;
xi: areg ln_recruitment comment gov_response after i.sector_id2 [aweight=_webal] if emp_size>4 & emp_size !=., absorb(pci_id) robust;
outreg2 using table_6, tdec(3) bdec(3) e(rmse ll) addtext(Ebalance, Yes, Province FE, Yes, Sector FE, Yes) excel;
pause;



/********************************Table 7. Mediation*****************************************/

/*Table 7: Models 1 and 2*/

#delimit;
set more off;
ebalance comment  emp_size a7_3 state_ownership equitized owner_connect former_hhe i.a4 a11_4 a11_5 a11_6 after i.pci_id i.sector_id2 after;

#delimit;
set more off;
sem (legitimacy <- gov_res )(formal <-legitimacy gov_response) [pweight=_webal], vce(robust);
outreg2 using table_7, tdec(3) bdec(3) e(all) addtext(Ebalance, No, Province FE, No, Sector FE, No) replace;
estat teffects;
pause;

/*Table 8: Column 1*/

/*Total Effect of Legitimacy*/
#delimit;
scalar define total_effect= (1.812876)+(0.5382259);
display total_effect;

/*Indirect Effect of Legitimacy*/
#delimit;
scalar define indirect_legit= .1957411*2.749683;
display indirect_legit;


/*Proportion of Total Response Effect Mediated by Legitimacy*/
#delimit;
scalar define ratio_total= indirect_legit/total_effect;
display ratio_total;



/*Ratio of Indirect to Direct Effect*/
#delimit;
scalar define total_response=indirect_legit/(total_effect-indirect_legit);
display total_response;
pause;


/*****************************Imai et al.*************************************/
/*Footnote Robust*/

#delimit;
medeff (regress legitimacy gov_resp) (regress formal legitimacy gov_resp) [pweight=_webal], treat(gov_resp) mediate(legitimacy) sims(1000);
outreg2 using table_7, tdec(3) bdec(3) e(all) addtext(Ebalance, Yes, Province FE, Yes, Sector FE, Yes);
pause;


/*****************************Compliance************************************/

/*Table 7: Models 3 and 4*/


#delimit;
sem (legitimacy <- gov_res)(compliance <-  legitimacy gov_response) [pweight=_webal], vce(robust);
outreg2 using table_7, tdec(3) bdec(3) e(all) addtext(Ebalance, Yes, Province FE, Yes, Sector FE, Yes) excel;
estat teffects;
pause;

/*Table 8: Column 2*/

/*Total Effect of Legitimacy*/
#delimit;
scalar define total_effect= (.0191248)+(.0343276);
display total_effect;

/*Indirect Effect of Legitimacy*/
#delimit;
scalar define indirect_legit= .1957411*.098;
display indirect_legit;


/*Proportion of Total Response Effect Mediated by Legitimacy*/
#delimit;
scalar define ratio_total= indirect_legit/total_effect;
display ratio_total;



/*Ratio of Indirect to Direct Effect*/
#delimit;
scalar define total_response=indirect_legit/(total_effect-indirect_legit);
display total_response;
pause;

/*****************************Imai et al.*************************************/


#delimit;
medeff (regress legitimacy gov_resp) (regress compliance legitimacy gov_resp) [pweight=_webal], treat(gov_resp) mediate(legitimacy) sims(1000);
outreg2 using table_7, tdec(3) bdec(3) e(all) addtext(Ebalance, Yes, Province FE, Yes, Sector FE, Yes);
pause;



/**************************Appendix Table A1*******************************/


/*Placebo Tests*/
#delimit;
set more off;
ebalance comment  emp_size a7_3  state_ownership equitized owner_connect former_hhe after;
xi: areg e14_1 comment gov_response after  i.sector_id2 [aweight=_webal] , robust absorb(pci_id);
outreg2 using table_A1, tdec(3) bdec(3) e(rmse ll) addtext(Ebalance, Yes, Province FE, Yes, Sector FE, Yes) replace;
xi: areg e14_2 comment gov_response after  i.sector_id2 [aweight=_webal] , robust absorb(pci_id);
outreg2 using table_A1, tdec(3) bdec(3) e(rmse ll) addtext(Ebalance, Yes, Province FE, Yes, Sector FE, Yes);
xi: areg e15_2 comment gov_response after  i.sector_id2 [aweight=_webal] , robust absorb(pci_id);
outreg2 using table_A1, tdec(3) bdec(3) e(rmse ll) addtext(Ebalance, Yes, Province FE, Yes, Sector FE, Yes);
xi: areg e20 comment gov_response after  i.sector_id2 [aweight=_webal] , robust absorb(pci_id);
outreg2 using table_A1, tdec(3) bdec(3) e(rmse ll) addtext(Ebalance, Yes, Province FE, Yes, Sector FE, Yes) excel;


/*************************************************************************************************************************************************************************************************************/
/*Table A2*/
/*Factor Analysis to Create Mediation Variables*/

#delimit;
drop legitimacy;
factor h1 d14_2 attitude d6 predict_reg, pcf;
rotate, varimax;
predict legitimacy information;
pause;


/*Table A3*/
#delimit;
set more off;
ebalance comment  emp_size a7_3 state_ownership equitized owner_connect former_hhe i.a4 a11_4 a11_5 a11_6 after i.pci_id i.sector_id2 after;

#delimit;
set more off;
sem (legitimacy <- gov_res )(information <- gov_response)(formal <- information legitimacy gov_response) [pweight=_webal], vce(robust);
outreg2 using table_a2, tdec(3) bdec(3) e(all) addtext(Ebalance, No, Province FE, No, Sector FE, No) replace;
estat teffects;
pause;


/*****************************Compliance************************************/



#delimit;
sem (legitimacy <- gov_res)(information <- gov_response)(compliance <- information legitimacy gov_response) [pweight=_webal], vce(robust);
outreg2 using table_a2, tdec(3) bdec(3) e(all) addtext(Ebalance, Yes, Province FE, Yes, Sector FE, Yes) excel;
estat teffects;



